# -*- coding: utf-8 -*-

"""
DateTime   : 2020/12/17 15:49
Author     : ZhangYafei
Description: 带参数的类装饰器
"""
import random
import time
from functools import wraps


class RecordTime:
    def __init__(self, num=4):
        self.num = num

    def __call__(self, func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            start = time.time()
            result = func(*args, **kwargs)
            end = time.time()
            print(f'{func.__name__}执行时间: {round(end - start, self.num)}秒')
            return result

        return wrapper


# 使用装饰器语法糖添加装饰器
@RecordTime(num=3)
def download(filename):
    print(f'开始下载{filename}.')
    time.sleep(random.randint(2, 6))
    print(f'{filename}下载完成.')


def upload(filename):
    print(f'开始上传{filename}.')
    time.sleep(random.randint(4, 8))
    print(f'{filename}上传完成.')


# 直接创建对象并调用对象传入被装饰的函数
upload = RecordTime()(upload)
download('MySQL从删库到跑路.avi')
upload('Python从入门到住院.pdf')
"""
开始下载MySQL从删库到跑路.avi.
MySQL从删库到跑路.avi下载完成.
download执行时间: 2.001秒
开始上传Python从入门到住院.pdf.
Python从入门到住院.pdf上传完成.
upload执行时间: 5.0002秒
"""